package com.crux.productor.controller;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;
@Slf4j
@RefreshScope
@RestController
@RequestMapping(value = "/api/demo")
public class DemoController {
    @Value("${sph.test}")
    private String test;

    @GetMapping(value = "/ping")
    @SneakyThrows
    public String pong(@RequestParam(required = false) String version){
        if("v1".equals(version)){
            TimeUnit.MILLISECONDS.sleep(1000);
        }else{
            TimeUnit.MILLISECONDS.sleep(2000);
        }
        return "pong";
    }

    @GetMapping(value = "/header")
    public String header(HttpServletRequest httpServletRequest){
        return httpServletRequest.getHeader("sph");
//        return httpServletRequest.getHeaderNames().toString();
    }

    @GetMapping(value = "test")
    public String test(){
        return this.test;
    }

    /**
     * 用于测试熔断，降级
     */
    @GetMapping(value = "/timeout")
    public String timeOut(){
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return "sleep 2s ";
    }
}
